1
|
|
|
/*! |
2
|
|
|
* @name ElkArte Forum |
3
|
|
|
* @copyright ElkArte Forum contributors |
4
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause |
5
|
|
|
* |
6
|
|
|
* @version 1.1 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This file contains javascript associated with the drag drop of files functionality |
11
|
|
|
* while posting |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Simply invoke the constructor by calling new dragDropAttachment |
16
|
|
|
*/ |
17
|
|
|
(function() { |
18
|
|
|
var dragDropAttachment = (function(params) { |
19
|
|
|
|
20
|
|
|
// Few internal global vars |
21
|
|
|
var allowedExtensions = [], |
22
|
|
|
curFileNum = 0, |
23
|
|
|
totalSizeAllowed = null, |
24
|
|
|
individualSizeAllowed = null, |
25
|
|
|
numOfAttachmentAllowed = null, |
26
|
|
|
totalAttachSizeUploaded = 0, |
27
|
|
|
numAttachUploaded = 0, |
28
|
|
|
filesUploadedSuccessfully = [], |
29
|
|
|
uploadInProgress = false, |
30
|
|
|
attachmentQueue = [], |
31
|
|
|
board = 0, |
32
|
|
|
topic = 0, |
33
|
|
|
oTxt = {}, |
34
|
|
|
errorMsg = '', |
35
|
|
|
// @deprecated since 1.1 - here just for backward compatibility |
36
|
|
|
fileDisplayTemplate = '<div class="statusbar"><div class="info"></div><div class="progressBar"><div></div></div><div class="control icon i-close"></div></div>', |
37
|
|
|
oEvents = {}, |
38
|
|
|
$str, |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* public function, accessible with prototype chain |
42
|
|
|
* @param {object} params |
43
|
|
|
* |
44
|
|
|
* allowedExtensions - types of attachments allowed |
45
|
|
|
* totalSizeAllowed - maximum size of total attachments allowed |
46
|
|
|
* individualSizeAllowed - maximum individual file size allowed |
47
|
|
|
* numOfAttachmentAllowed - number of files that can be attached in a post |
48
|
|
|
* totalAttachSizeUploaded - total size of already attached files(modifying post) |
49
|
|
|
* numAttachUploaded - number of already attached files(modifying post) |
50
|
|
|
*/ |
51
|
|
|
init = function(params) { |
52
|
|
|
if (typeof params.events !== 'undefined') |
53
|
|
|
{ |
54
|
|
|
for (var event in params.events) { |
55
|
|
|
if (params.events.hasOwnProperty(event)) { |
56
|
|
|
addEventListener(event, params.events[event]); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
allowedExtensions = (params.allowedExtensions === '') ? [] : params.allowedExtensions.toLowerCase().replace(/\s/g, '').split(','); |
62
|
|
|
totalSizeAllowed = (params.totalSizeAllowed === '') ? null : params.totalSizeAllowed; |
63
|
|
|
individualSizeAllowed = (params.individualSizeAllowed === '') ? null : params.individualSizeAllowed; |
64
|
|
|
numOfAttachmentAllowed = (params.numOfAttachmentAllowed === '') ? null : params.numOfAttachmentAllowed; |
65
|
|
|
totalAttachSizeUploaded = params.totalAttachSizeUploaded / 1024; |
66
|
|
|
numAttachUploaded = params.numAttachUploaded; |
67
|
|
|
filesUploadedSuccessfully = []; |
68
|
|
|
if (typeof params.topic !== 'undefined') |
69
|
|
|
topic = params.topic; |
70
|
|
|
if (typeof params.fileDisplayTemplate !== 'undefined') |
71
|
|
|
fileDisplayTemplate = params.fileDisplayTemplate; |
72
|
|
|
$str = $(fileDisplayTemplate); |
73
|
|
|
board = params.board; |
74
|
|
|
oTxt = params.oTxt; |
75
|
|
|
if (typeof params.existingSelector !== 'undefined') |
76
|
|
|
processExisting($(params.existingSelector)); |
77
|
|
|
}, |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* private function |
81
|
|
|
* |
82
|
|
|
* Takes already uploaded files (e.g. when editing a message) |
83
|
|
|
* and creates the "D&D" interface |
84
|
|
|
* |
85
|
|
|
* @param {object} $files Array of elements of existing input files |
86
|
|
|
*/ |
87
|
|
|
processExisting = function ($files) { |
88
|
|
|
$files.each(function(idx, value) { |
|
|
|
|
89
|
|
|
var status = new createStatusbar({}), |
90
|
|
|
$file = $(this); |
91
|
|
|
|
92
|
|
|
status.setFileNameSize($file.parent().text(), $file.data('size')); |
93
|
|
|
status.setProgress(100); |
94
|
|
|
|
95
|
|
|
var $button = status.getButton(), |
96
|
|
|
data = { |
97
|
|
|
curFileNum: curFileNum++, |
98
|
|
|
attachid: $file.data('attachid'), |
99
|
|
|
size: $file.data('size') |
100
|
|
|
}; |
101
|
|
|
$button.addClass('abort'); |
102
|
|
|
status.onUploadSuccess(data); |
103
|
|
|
filesUploadedSuccessfully.push(data); |
104
|
|
|
$file.closest('dd').remove(); |
105
|
|
|
}); |
106
|
|
|
}, |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* private function |
110
|
|
|
* |
111
|
|
|
* Uploads the file to server and updates the UI |
112
|
|
|
* |
113
|
|
|
* @param {object} formData current file with data to upload |
114
|
|
|
* @param {object} status current progress bar UI instance |
115
|
|
|
* @param {int} fileSize current progress bar UI instance |
116
|
|
|
* @param {string} fileName current progress bar UI instance |
117
|
|
|
*/ |
118
|
|
|
sendFileToServer = function(formData, status, fileSize, fileName) { |
119
|
|
|
var jqXHR = $.ajax({ |
120
|
|
|
xhr: function() { |
121
|
|
|
var xhrobj = $.ajaxSettings.xhr(); |
122
|
|
|
|
123
|
|
|
if (xhrobj.upload) { |
124
|
|
|
// Set up a progress event listener to update the UI |
125
|
|
|
xhrobj.upload.addEventListener('progress', function(event) { |
126
|
|
|
var percent = 0, |
127
|
|
|
position = event.loaded || event.position, |
128
|
|
|
total = event.total; |
129
|
|
|
|
130
|
|
|
if (event.lengthComputable) |
131
|
|
|
percent = Math.ceil(position / total * 100); |
132
|
|
|
|
133
|
|
|
status.setProgress(percent); |
134
|
|
|
}, false); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return xhrobj; |
138
|
|
|
}, |
139
|
|
|
url: elk_scripturl + '?action=attachment;sa=ulattach;api;' + elk_session_var + '=' + elk_session_id + ';board=' + board, |
|
|
|
|
140
|
|
|
type: "POST", |
141
|
|
|
dataType: "json", |
142
|
|
|
contentType: false, |
143
|
|
|
processData: false, |
144
|
|
|
cache: false, |
145
|
|
|
data: formData, |
146
|
|
|
context: { |
147
|
|
|
'fileName': fileName, |
148
|
|
|
'fileSize': fileSize |
149
|
|
|
} |
150
|
|
|
}).done(function(resp) { |
151
|
|
|
if (typeof(resp) !== 'object') |
152
|
|
|
resp = JSON.parse(resp); |
153
|
|
|
|
154
|
|
|
// Well its done, lets make sure the server says so as well |
155
|
|
|
if (resp.result) { |
156
|
|
|
var curFileNum = filesUploadedSuccessfully.length, |
157
|
|
|
data = resp.data; |
158
|
|
|
|
159
|
|
|
// Show its done |
160
|
|
|
status.setProgress(100); |
161
|
|
|
filesUploadedSuccessfully.push(data); |
162
|
|
|
data.curFileNum = curFileNum; |
163
|
|
|
status.onUploadSuccess(data); |
164
|
|
|
} else { |
165
|
|
|
// The server was unable to process the file, show it as not sent |
166
|
|
|
var errorMsgs = {}, |
167
|
|
|
serverErrorFiles = []; |
168
|
|
|
|
169
|
|
|
for (var err in resp.data) { |
170
|
|
|
if (resp.data.hasOwnProperty(err)) { |
171
|
|
|
errorMsgs.individualServerErr = resp.data[err].title.php_unhtmlspecialchars() + '<br />'; |
172
|
|
|
|
173
|
|
|
for (var errMsg in resp.data[err].errors) { |
174
|
|
|
if (resp.data[err].errors.hasOwnProperty(errMsg)) |
175
|
|
|
serverErrorFiles.push(resp.data[err].errors[errMsg].php_unhtmlspecialchars()); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
numAttachUploaded--; |
|
|
|
|
179
|
|
|
|
180
|
|
|
populateErrors({ |
181
|
|
|
'errorMsgs': errorMsgs, |
182
|
|
|
'serverErrorFiles': serverErrorFiles |
183
|
|
|
}); |
184
|
|
|
} |
185
|
|
|
status.setServerFail(0); |
186
|
|
|
} |
187
|
|
|
}).fail(function(e, textStatus, errorThrown) { |
|
|
|
|
188
|
|
|
var errorMsgs = {}, |
189
|
|
|
sizeErrorFiles = []; |
190
|
|
|
|
191
|
|
|
numAttachUploaded--; |
192
|
|
|
errorMsgs.individualSizeErr = oTxt.postUploadError; |
193
|
|
|
sizeErrorFiles.push(this.fileName.php_htmlspecialchars()); |
194
|
|
|
populateErrors({ |
195
|
|
|
'errorMsgs': errorMsgs, |
196
|
|
|
'sizeErrorFiles': sizeErrorFiles |
197
|
|
|
}); |
198
|
|
|
status.setServerFail(0); |
199
|
|
|
|
200
|
|
|
}).always(function() { |
201
|
|
|
uploadInProgress = false; |
202
|
|
|
runAttachmentQueue(); |
203
|
|
|
}); |
204
|
|
|
status.setAbort(jqXHR); |
205
|
|
|
}, |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* private function |
209
|
|
|
* |
210
|
|
|
* Removes the file from the server that was successfully uploaded |
211
|
|
|
* |
212
|
|
|
* @param {object} options |
213
|
|
|
*/ |
214
|
|
|
removeFileFromServer = function(options) { |
215
|
|
|
var dataToSend = filesUploadedSuccessfully[options.fileNum]; |
216
|
|
|
|
217
|
|
|
// So you did not want to send that file? |
218
|
|
|
$.ajax({ |
219
|
|
|
url: elk_scripturl + '?action=attachment;sa=rmattach;api;' + elk_session_var + '=' + elk_session_id, |
|
|
|
|
220
|
|
|
type: "POST", |
221
|
|
|
cache: false, |
222
|
|
|
dataType: 'json', |
223
|
|
|
data: { |
224
|
|
|
'attachid': dataToSend.attachid |
225
|
|
|
} |
226
|
|
|
}).done(function(resp) { |
227
|
|
|
if (typeof(resp) !== 'object') |
228
|
|
|
resp = JSON.parse(resp); |
229
|
|
|
|
230
|
|
|
// Make sure we have a result:true in the response |
231
|
|
|
if (resp.result) { |
232
|
|
|
// Update our counters, number of files allowed and total data payload |
233
|
|
|
totalAttachSizeUploaded -= filesUploadedSuccessfully[options.fileNum].size / 1024; |
234
|
|
|
numAttachUploaded--; |
235
|
|
|
triggerEvt('RemoveSuccess', options.control, [dataToSend.attachid]); |
236
|
|
|
|
237
|
|
|
// Done with this one, so remove it from existence |
238
|
|
|
$('#' + dataToSend.attachid).off().remove(); |
239
|
|
|
} else if ('console' in window) |
240
|
|
|
window.console.info(resp.data); |
241
|
|
|
}).fail(function(jqXHR, textStatus, errorThrown) { |
242
|
|
|
if ('console' in window) { |
243
|
|
|
window.console.info('Error:', textStatus, errorThrown.name); |
244
|
|
|
window.console.info(jqXHR.responseText); |
245
|
|
|
} |
246
|
|
|
}); |
247
|
|
|
}, |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* private function |
251
|
|
|
* |
252
|
|
|
* Creates the status UI for each file dropped |
253
|
|
|
* Initiate as new createStatusbar |
254
|
|
|
* Has the following methods available to it |
255
|
|
|
* - setFileNameSize |
256
|
|
|
* - setProgress |
257
|
|
|
* - setAbort |
258
|
|
|
* - setServerFail |
259
|
|
|
* - onUploadSuccess |
260
|
|
|
* @param {object} obj options |
261
|
|
|
*/ |
262
|
|
|
createStatusbar = function(obj) { |
|
|
|
|
263
|
|
|
var $control = $str.clone(), |
264
|
|
|
$button = $control.find('.control'), |
265
|
|
|
$progressbar = $control.find('.progressBar'); |
266
|
|
|
$button.addClass('abort'); |
267
|
|
|
|
268
|
|
|
$('.progress_tracker').append($control); |
269
|
|
|
|
270
|
|
|
// Provide the file size in something more legible, like 100KB or 1.1MB |
271
|
|
|
this.setFileNameSize = function(name, size) { |
272
|
|
|
var sizeStr = "", |
273
|
|
|
sizeKB = size / 1024, |
274
|
|
|
sizeMB = sizeKB / 1024; |
275
|
|
|
|
276
|
|
|
if (parseInt(sizeKB, 10) > 1024) |
277
|
|
|
sizeStr = sizeMB.toFixed(2) + " MB"; |
278
|
|
|
else |
279
|
|
|
sizeStr = sizeKB.toFixed(2) + " KB"; |
280
|
|
|
|
281
|
|
|
$control.find('.info').html(name + ' (' + sizeStr + ')'); |
282
|
|
|
}; |
283
|
|
|
|
284
|
|
|
// Set the progress bar position |
285
|
|
|
this.setProgress = function(progress) { |
286
|
|
|
var /*$progressbar = $control.find('.progressBar'),*/ |
287
|
|
|
progressBarWidth = progress * $progressbar.width() / 100; |
288
|
|
|
|
289
|
|
|
$progressbar.find('div').animate({ |
290
|
|
|
width: progressBarWidth |
291
|
|
|
}, 10).html(progress + "% "); |
292
|
|
|
}; |
293
|
|
|
|
294
|
|
|
// Provide a way to stop the upload before its done |
295
|
|
|
this.setAbort = function(jqxhr) { |
296
|
|
|
var sb = $control; |
297
|
|
|
|
298
|
|
|
$button.on('click', function(e) { |
299
|
|
|
e.preventDefault(); |
300
|
|
|
jqxhr.abort(); |
301
|
|
|
sb.hide(); |
302
|
|
|
}); |
303
|
|
|
}; |
304
|
|
|
|
305
|
|
|
this.getButton = function() { |
306
|
|
|
return $button; |
307
|
|
|
}; |
308
|
|
|
|
309
|
|
|
// Server Failure is always an option when sending files |
310
|
|
|
this.setServerFail = function(data) { |
|
|
|
|
311
|
|
|
this.setProgress(0); |
312
|
|
|
$button.removeClass('i-close').addClass('i-alert'); |
313
|
|
|
}; |
314
|
|
|
|
315
|
|
|
// The file upload is successful, remove our abort event and swap the class |
316
|
|
|
this.onUploadSuccess = function(data) { |
317
|
|
|
fileUploadedInterface($control, $button, data); |
318
|
|
|
triggerEvt('UploadSuccess', $control, [$button, data]); |
319
|
|
|
}; |
320
|
|
|
}, |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* private function |
324
|
|
|
* |
325
|
|
|
* Prepares the uploaded file area |
326
|
|
|
* |
327
|
|
|
* @param {object} $control |
328
|
|
|
* @param {object} $button |
329
|
|
|
* @param {object} data |
330
|
|
|
*/ |
331
|
|
|
fileUploadedInterface = function($control, $button, data) { |
332
|
|
|
$button.off('click'); |
333
|
|
|
$button.removeClass('abort i-close').addClass('remove i-delete'); |
334
|
|
|
|
335
|
|
|
// Update the uploaded file with its ID |
336
|
|
|
$button.attr('id', data.curFileNum); |
337
|
|
|
$control.attr('id', data.attachid); |
338
|
|
|
$control.attr('data-size', data.size); |
339
|
|
|
|
340
|
|
|
// We need to tell Elk that the file should not be deleted |
341
|
|
|
$button.after($('<input />') |
342
|
|
|
.attr('type', 'hidden') |
343
|
|
|
.attr('name', 'attach_del[]') |
344
|
|
|
.attr('value', data.attachid)); |
345
|
|
|
|
346
|
|
|
var $img = $('<img />').attr('src', elk_scripturl + '?action=dlattach;sa=tmpattach;attach=' + $control.attr('id') + ';topic=' + topic), |
|
|
|
|
347
|
|
|
$progressbar = $control.find('.progressBar'); |
348
|
|
|
$progressbar.after($('<div class="postattach_thumb" />').append($img)); |
349
|
|
|
$progressbar.remove(); |
350
|
|
|
|
351
|
|
|
// Provide a way to remove a file that has been sent by mistake |
352
|
|
|
$button.on('click', function(e) { |
353
|
|
|
e.preventDefault(); |
354
|
|
|
|
355
|
|
|
var fileNum = e.target.id; |
356
|
|
|
|
357
|
|
|
if (confirm(oTxt.areYouSure)) |
|
|
|
|
358
|
|
|
{ |
359
|
|
|
removeFileFromServer({ |
360
|
|
|
'fileNum': fileNum, |
361
|
|
|
'control': $control |
362
|
|
|
}); |
363
|
|
|
} |
364
|
|
|
}); |
365
|
|
|
}, |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* public function |
369
|
|
|
* |
370
|
|
|
* Handle the functionality when file(s) are dropped |
371
|
|
|
* |
372
|
|
|
* @param {object} files what files to upload |
373
|
|
|
* @param {object} obj parent object in which file progress is shown |
374
|
|
|
*/ |
375
|
|
|
handleFileUpload = function(files, obj) { |
376
|
|
|
var errorMsgs = {}, |
377
|
|
|
extnErrorFiles = [], |
378
|
|
|
sizeErrorFiles = []; |
379
|
|
|
|
380
|
|
|
for (var i = 0; i < files.length; i++) { |
381
|
|
|
var fileExtensionCheck = /(?:\.([^.]+))?$/, |
382
|
|
|
extension = fileExtensionCheck.exec(files[i].name)[1].toLowerCase(), |
383
|
|
|
fileSize = files[i].size / 1024, |
384
|
|
|
errorFlag = false; |
385
|
|
|
|
386
|
|
|
// Make sure the server will allow this type of file |
387
|
|
|
if (allowedExtensions.length > 0 && allowedExtensions.indexOf(extension) < 0) { |
388
|
|
|
errorMsgs.extnError = '(<strong>' + extension + '</strong>) ' + oTxt.allowedExtensions; |
389
|
|
|
extnErrorFiles.push(files[i].name); |
390
|
|
|
errorFlag = true; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
// Make sure the file is not larger than the server will accept |
394
|
|
|
if (individualSizeAllowed !== null && fileSize > individualSizeAllowed) { |
395
|
|
|
errorMsgs.individualSizeErr = '(' + parseInt(fileSize, 10) + ' KB) ' + oTxt.individualSizeAllowed; |
396
|
|
|
sizeErrorFiles.push(files[i].name); |
397
|
|
|
errorFlag = true; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
// And you can't send too many |
401
|
|
|
if (numAttachUploaded >= numOfAttachmentAllowed) { |
|
|
|
|
402
|
|
|
errorMsgs.maxNumErr = oTxt.numOfAttachmentAllowed; |
403
|
|
|
sizeErrorFiles.push(files[i].name); |
404
|
|
|
errorFlag = true; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
// Lets see if this will exceed the total file quota we allow |
408
|
|
|
if (errorFlag === false) |
409
|
|
|
totalAttachSizeUploaded += fileSize; |
|
|
|
|
410
|
|
|
|
411
|
|
|
if (totalSizeAllowed !== null && totalAttachSizeUploaded > totalSizeAllowed) { |
412
|
|
|
errorMsgs.totalSizeError = oTxt.totalSizeAllowed.replace("%1$s", totalSizeAllowed).replace("%2$s", String(parseInt(totalSizeAllowed - (totalAttachSizeUploaded - fileSize), 10))); |
413
|
|
|
errorFlag = true; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
// No errors, so update the counters (number, total size, etc) |
417
|
|
|
// and add this file to the processing queue |
418
|
|
|
if (errorFlag === false) { |
419
|
|
|
var fd = new FormData(), |
420
|
|
|
status = new createStatusbar(obj); |
421
|
|
|
|
422
|
|
|
numAttachUploaded++; |
423
|
|
|
fd.append('attachment[]', files[i]); |
424
|
|
|
status.setFileNameSize(files[i].name.php_htmlspecialchars(), files[i].size); |
425
|
|
|
attachmentQueue.push({ |
426
|
|
|
'formData': fd, |
427
|
|
|
'statusInstance': status, |
428
|
|
|
'fileName': files[i].name, |
429
|
|
|
'fileSize': files[i].size |
430
|
|
|
}); |
431
|
|
|
} |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
// Time to show any errors for this batch of files |
435
|
|
|
populateErrors({ |
436
|
|
|
'errorMsgs': errorMsgs, |
437
|
|
|
'extnErrorFiles': extnErrorFiles, |
438
|
|
|
'sizeErrorFiles': sizeErrorFiles |
439
|
|
|
}); |
440
|
|
|
|
441
|
|
|
runAttachmentQueue(); |
442
|
|
|
}, |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* private function |
446
|
|
|
* |
447
|
|
|
* Checks if there are any files pending upload |
448
|
|
|
*/ |
449
|
|
|
runAttachmentQueue = function() { |
450
|
|
|
if (attachmentQueue.length > 0 && uploadInProgress === false) { |
451
|
|
|
setTimeout(function () { |
452
|
|
|
var currentData = attachmentQueue[0]; |
453
|
|
|
|
454
|
|
|
uploadInProgress = true; |
455
|
|
|
sendFileToServer(currentData.formData, currentData.statusInstance, currentData.fileSize, currentData.fileName); |
456
|
|
|
attachmentQueue.splice(0, 1); |
457
|
|
|
}, 200); |
458
|
|
|
} |
459
|
|
|
}, |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* private function |
463
|
|
|
* |
464
|
|
|
* Populates the warning box when something does not go as expected |
465
|
|
|
* |
466
|
|
|
* @param {object} params |
467
|
|
|
* error messages to show |
468
|
|
|
* file names having extension error |
469
|
|
|
* file names having size error |
470
|
|
|
*/ |
471
|
|
|
populateErrors = function(params) { |
472
|
|
|
var $drop_attachments_error = $('.drop_attachments_error'); |
473
|
|
|
|
474
|
|
|
$drop_attachments_error.html(''); |
475
|
|
|
|
476
|
|
|
var wrapper = '<p class="warningbox">'; |
477
|
|
|
|
478
|
|
|
for (var err in params.errorMsgs) { |
479
|
|
|
if (params.errorMsgs.hasOwnProperty(err)) { |
480
|
|
|
// Build the warning box of errors this file generated |
481
|
|
|
switch (err) { |
482
|
|
|
case 'extnError': |
483
|
|
|
errorMsg = wrapper + params.extnErrorFiles.join(', ') + ' : ' + params.errorMsgs[err] + '</p>'; |
484
|
|
|
break; |
485
|
|
|
case 'individualSizeErr': |
486
|
|
|
errorMsg = wrapper + params.sizeErrorFiles.join(', ') + ' : ' + params.errorMsgs[err] + '</p>'; |
487
|
|
|
break; |
488
|
|
|
case 'individualServerErr': |
489
|
|
|
errorMsg = wrapper + params.errorMsgs[err] + params.serverErrorFiles.join(', ') + '</p>'; |
490
|
|
|
break; |
491
|
|
|
default: |
492
|
|
|
errorMsg = wrapper + params.errorMsgs[err] + '</p>'; |
493
|
|
|
break; |
494
|
|
|
} |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
// Show them what they are doing wrong |
498
|
|
|
$drop_attachments_error.append(errorMsg); |
|
|
|
|
499
|
|
|
} |
500
|
|
|
}, |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* private function |
504
|
|
|
* |
505
|
|
|
* Used to check if a value exists in an array |
506
|
|
|
* |
507
|
|
|
* @param {string} needle |
508
|
|
|
*/ |
509
|
|
|
indexOf = function(needle) { |
|
|
|
|
510
|
|
|
if (typeof Array.prototype.indexOf === 'function') |
511
|
|
|
indexOf = Array.prototype.indexOf; |
512
|
|
|
else { |
513
|
|
|
indexOf = function(needle) { |
514
|
|
|
var i, |
515
|
|
|
index = -1; |
516
|
|
|
|
517
|
|
|
for (i = 0; i < this.length; i++) { |
518
|
|
|
if (this[i] === needle) { |
519
|
|
|
index = i; |
520
|
|
|
break; |
521
|
|
|
} |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
return index; |
525
|
|
|
}; |
526
|
|
|
} |
527
|
|
|
}, |
528
|
|
|
|
529
|
|
|
/** |
530
|
|
|
* public function |
531
|
|
|
* |
532
|
|
|
* Used to extend the code |
533
|
|
|
* |
534
|
|
|
* @param {string} event |
535
|
|
|
* @param {object} listener |
536
|
|
|
*/ |
537
|
|
|
addEventListener = function(event, listener) { |
538
|
|
|
if (!oEvents.hasOwnProperty(event)) |
539
|
|
|
oEvents[event] = []; |
540
|
|
|
|
541
|
|
|
oEvents[event].push(listener); |
542
|
|
|
}, |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* private function |
546
|
|
|
* |
547
|
|
|
* Runs all the listeners on a certain event |
548
|
|
|
* |
549
|
|
|
* @param {string} event |
550
|
|
|
* @param {object} aThis |
551
|
|
|
* @param {object} args |
552
|
|
|
*/ |
553
|
|
|
triggerEvt = function(event, aThis, args) { |
554
|
|
|
if (!oEvents.hasOwnProperty(event)) |
555
|
|
|
return; |
556
|
|
|
|
557
|
|
|
for (var i = 0; i < oEvents[event].length; i++) { |
558
|
|
|
oEvents[event][i].apply(aThis, args); |
559
|
|
|
} |
560
|
|
|
}; |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* Initialize the drag and drop function! |
564
|
|
|
*/ |
565
|
|
|
$(function() { |
566
|
|
|
var obj = $(".drop_area"); |
567
|
|
|
|
568
|
|
|
// Make sure the browser supports this |
569
|
|
|
if (!(window.FormData && ("onprogress" in $.ajaxSettings.xhr()))) |
570
|
|
|
return; |
571
|
|
|
|
572
|
|
|
// All clear, show the drop zone |
573
|
|
|
obj.toggle(); |
574
|
|
|
$('.drop_attachments_no_js').hide(); |
575
|
|
|
|
576
|
|
|
// Entering the dropzone, show it |
577
|
|
|
obj.on('dragenter', function(e) { |
578
|
|
|
e.stopPropagation(); |
579
|
|
|
e.preventDefault(); |
580
|
|
|
$(this).css('opacity', '1'); |
581
|
|
|
}); |
582
|
|
|
|
583
|
|
|
// Hovering over, waiting waiting waiting, show we are waiting |
584
|
|
|
obj.on('dragover', function(e) { |
585
|
|
|
e.stopPropagation(); |
586
|
|
|
e.preventDefault(); |
587
|
|
|
}); |
588
|
|
|
|
589
|
|
|
// Catch what you dropped, and send it off to be processed |
590
|
|
|
obj.on('drop', function(e) { |
591
|
|
|
var files = e.originalEvent.dataTransfer.files; |
592
|
|
|
|
593
|
|
|
e.preventDefault(); |
594
|
|
|
$(this).css('opacity', '0.6'); |
595
|
|
|
handleFileUpload(files, obj); |
596
|
|
|
}); |
597
|
|
|
|
598
|
|
|
// Wait, where are you going? Lets show you are outside the zone |
599
|
|
|
obj.on('dragexit', function(e) { |
600
|
|
|
e.preventDefault(); |
601
|
|
|
$(this).css('opacity', '0.6'); |
602
|
|
|
}); |
603
|
|
|
|
604
|
|
|
// Rather click and select? |
605
|
|
|
$input = obj.find('#attachment_click'); |
|
|
|
|
606
|
|
|
$input.change(function(e) { |
607
|
|
|
e.preventDefault(); |
608
|
|
|
var files = $(this)[0].files; |
609
|
|
|
handleFileUpload(files, obj); |
610
|
|
|
this.value = null; |
611
|
|
|
}); |
612
|
|
|
$input.clone(true, true).appendTo('.drop_area_fileselect_text'); |
613
|
|
|
$input.hide(); |
614
|
|
|
}); |
615
|
|
|
init(params); |
616
|
|
|
return { |
617
|
|
|
init: init, |
618
|
|
|
addEventListener: addEventListener, |
619
|
|
|
handleFileUpload: handleFileUpload |
620
|
|
|
}; |
621
|
|
|
}); |
622
|
|
|
|
623
|
|
|
// AMD / RequireJS |
624
|
|
|
if ( typeof define !== 'undefined' && define.amd) { |
|
|
|
|
625
|
|
|
define([], function() { |
626
|
|
|
return dragDropAttachment; |
627
|
|
|
}); |
628
|
|
|
} |
629
|
|
|
// CommonJS |
630
|
|
|
else if ( typeof module !== 'undefined' && module.exports) { |
631
|
|
|
module.exports = dragDropAttachment; |
632
|
|
|
} |
633
|
|
|
// included directly via <script> tag |
634
|
|
|
else { |
635
|
|
|
this.dragDropAttachment = dragDropAttachment; |
636
|
|
|
} |
637
|
|
|
}()); |
638
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.